package com.llxz.blog.article.controller;

import com.llxz.blog.article.dto.CollectParam;
import com.llxz.blog.article.entity.Article;
import com.llxz.blog.article.service.CollectService;
import com.llxz.blog.article.util.JsonResult;
import com.llxz.blog.article.util.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "article")
public class CollectionController {

    @Autowired
    private CollectService collectService;

    @PostMapping(value = "/collect")
    public JsonResult collectArticle(@RequestBody CollectParam collectParam) {
        collectService.collectArticle(collectParam);
        return new JsonResult();
    }

    @PostMapping(value = "/collect/get/{phone}")
    public JsonResult getCollection(@PathVariable String phone, Pageable pageable) {
        List<Article> list = collectService.getOnesCollection(phone);
        if (list == null) {
            return new JsonResult();
        } else {
            Page<Article> pages =  new PageHelper().listConvertToPage(list,pageable);
            return new JsonResult(pages);
        }
    }

    @PostMapping(value = "/collect/cancel")
    public JsonResult cancelCollection(@RequestBody  CollectParam collectParam) {
        collectService.cancelCollection(collectParam);
        return new JsonResult();
    }

    @PostMapping(value = "/collect/if")
    public boolean ifCollect(@RequestBody CollectParam collectParam) {
        boolean flag = collectService.ifCollect(collectParam);
        return flag;
    }
}
